﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Manager.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using NHibernate.Criterion;

namespace ZDSoft.UPMS.Component.BusinessPart
{
    public class SituationTypeComponent : ComponentBase<SituationType, SituationTypeManager>, ISituationTypeService
    {
        /// <summary>
        /// 创建情况类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pid">父节点ID</param>
        /// <param name="name">节点名称</param>
        /// <returns></returns>
        public int Create(string id, string pid, string name)
        {
            SituationType situationType = new SituationType();
            IList<SituationType> sType = base.FindAll();
            bool flag = true;
            for (int i = 0; i < sType.Count; i++)
            {
                if (sType[i].ID == int.Parse(id))
                {

                    flag = false;
                    break;
                }
            }
            if (flag == true)
            {
                situationType.Name = name;
                if (pid != "")
                {
                    situationType.Parent = base.FindByID(int.Parse(pid));
                }
                base.Create(situationType);

            }
            else
            {
                ///修改节点信息
                situationType = FindByID(int.Parse(id));
                situationType.Name = name;
                base.Update(situationType);
            }

            return situationType.ID;
        }

        /// <summary>
        /// 查找情况类型的叶子节点
        /// </summary>
        IList<SituationType> leafNode = new List<SituationType>();
        public IList<SituationType> FindSituationTypeLeaves()
        {

            FindLeafeNode(base.FindAll().Where(o => o.Parent == null).First());
            return leafNode;
        }
        private void FindLeafeNode(SituationType situationType)
        {
            if (situationType.Children == null || situationType.Children.Count == 0)
            {
                leafNode.Add(situationType);
            }
            else
            {
                foreach (SituationType sType in situationType.Children)
                {
                    FindLeafeNode(sType);
                }
            }
        }

        /// <summary>
        /// 查找情况类型的根节点
        /// </summary>
        /// <returns></returns>
        public IList<SituationType> FindSituationTypeParents()
        {
            IList<SituationType> siTypeList = base.FindAll();
            IList<SituationType> parents = new List<SituationType>();
            for (int i = 0; i < siTypeList.Count; i++)
            {
                if (siTypeList[i].Parent == null)
                {
                    parents.Add(siTypeList[i]);
                }
            }

            return parents;
        }
    }
}
